<?php
// +----------------------------------------------------------------------
// |     【BillMain】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class BillMain extends Model{
    use SoftDelete;
     protected $deleteTime = false;
    // 获取列表
    public static function getList($where = ''){
        $limit = I('get.limit/d');
		$type = I("type",-1);
		$year = I("year",0);
		$month = I("month",0);
		$day = I("day",0);
		$addtype = I("sg",-1);
		
		//按收支项目查找
		if ($name = I("name")) 	$where .= " and (name like '%" . $name . "%' or remark like '%" . $name . "%')";
		if ($year >0) 	$where .= " and year=" . $year;
		if ($year >0 && $month >0)	$where .= " and month=" . $month;
		if ($year >0 && $month >0 && $day >0) 	$where .= " and day=" . $day;
		if ($addtype != -1 ) $where .= " and addtype=" . $addtype;
		if ($type != -1)	$where .= " and type=" . $type;

		if(($year==0 && $month!=0) || (($year==0 || $month==0) && $day!=0)){
			return ['msg'=>'选择日期格式不正确','code'=>201];
		}

        $list = self::order('id','desc')->where($where)->append(['addtype_text'])->paginate($limit);
		$total = self::where($where)->sum('money');
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit,'total'=>price($total)]];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();
		
		return $data;
	}
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::find($id);
		}else{
			$model=self::where($id)->find();
		}
		return $model;		
	}
	
	/** 获取器 **/
	public function getCreateTimeAttr($value,$data){
		return date('Y-m-d',$data['create_time']);	
	}
	
	public function getAddtypeTextAttr($value,$data){
		$config=config_cache_file('options.bill_addtype');
		return $config[$data['addtype']]['val'];;		
	}
}
